﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DataLayer;
using System.Threading;
using System.Windows.Threading;
namespace LuanVanProject
{
    /// <summary>
    /// Interaction logic for ExportConfig.xaml
    /// </summary>
    public partial class ExportConfig : Window
    {
        Node exportNode = null;
        string name = "export v1.pptx";
        public ExportConfig(Node _node)
        {
            InitializeComponent();
            exportNode = _node;
            this.tbPath.Text = App.ProjectDirectory + "\\Outputs\\" + name;
            this.ThemeChooser.ExportWindow = this;
        }


        public ExportConfig(Node _node,string _name)
        {
            InitializeComponent();
            exportNode = _node;
            name = _name;
            this.tbPath.Text = App.ProjectDirectory + "\\Outputs\\" + name;
            this.ThemeChooser.ExportWindow = this;
        }

        private void savefile_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.SaveFileDialog savefileDlg = new System.Windows.Forms.SaveFileDialog();
          System.Windows.Forms.DialogResult rs =  savefileDlg.ShowDialog();
          if (rs == System.Windows.Forms.DialogResult.OK)
          {
              tbPath.Text = savefileDlg.FileName;
          }
        }

        private void btExport_Click(object sender, RoutedEventArgs e)
        {            
           
            Export();
            //App.mainWindow.MyProjectPane.LoadOutputFile();
        }

        MindMap mm;
        string path;
        int level = 0;
        string strThemeName;
        Thread t = null;
        int index = 1;
        public void Export()
        {
            mm = new MindMap(exportNode);
            path = tbPath.Text;
            
            strThemeName = themeName.Content as string;
            if (rBAll.IsChecked == true)
            {
                t = new Thread(new ThreadStart(ExportMindMap2PP));
            }
            if (rBLevel.IsChecked == true)
            {
                try
                {
                     level = cbLevel.SelectedIndex + 1;
                     t = new Thread(new ThreadStart(ExportMindMap2PPByLevel));
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Vui lòng nhập số vào ô level");
                    return;
                }
            }
            if (rBIndex.IsChecked == true)
            {
                try
                {
                    index = int.Parse(tbIndex.Text);
                    t = new Thread(new ThreadStart(ExportMindMap2PPWithIndex));
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Please enter number to box");
                    tbIndex.Text = "";
                    return;
                }
            }
           
            t.Start();
            this.btExport.IsEnabled = false;
           // this.btCancel.IsEnabled = false;
        }

        public void SetThemeName(string themeName)
        {
            this.themeName.Content = themeName;
        }

        void ExportMindMap2PPByLevel()
        {
            try
            {
                mm.ExportToPP(path, strThemeName, level);
                Thread closeThread = new Thread(
                        new ThreadStart(
                            delegate()
                            {
                                DispatcherOperation dispOp =
                                    pgBar.Dispatcher.BeginInvoke(DispatcherPriority.Loaded,
                                    new Action(
                                        delegate()
                                        {
                                            App.mainWindow.MyProjectPane.LoadOutputFile();
                                            this.Close();

                                        }));
                            }));

                closeThread.Start();
            }
            catch
            {
                MessageBox.Show("Export process occur errors, probably because your computer is not installed Microsoft Powerpoint, please install the 2007 version and try again");
            }
        }

        void ExportMindMap2PP()
        {
            try
            {
                mm.ExportToPP(path, strThemeName);
                Thread closeThread = new Thread(
                       new ThreadStart(
                           delegate()
                           {
                               DispatcherOperation dispOp =
                                   pgBar.Dispatcher.BeginInvoke(DispatcherPriority.Loaded,
                                   new Action(
                                       delegate()
                                       {
                                           App.mainWindow.MyProjectPane.LoadOutputFile();
                                           this.Close();
                                       }));
                           }));

                closeThread.Start();
            }
            catch {
                MessageBox.Show("Export process occur errors, probably because your computer is not installed Microsoft Powerpoint, please install the 2007 version and try again");
            }
        }

        void ExportMindMap2PPWithIndex()
        {
            try
            {
                mm.InsertExportProcess(path, strThemeName,index);
                Thread closeThread = new Thread(
                       new ThreadStart(
                           delegate()
                           {
                               DispatcherOperation dispOp =
                                   pgBar.Dispatcher.BeginInvoke(DispatcherPriority.Loaded,
                                   new Action(
                                       delegate()
                                       {
                                           App.mainWindow.MyProjectPane.LoadOutputFile();
                                           this.Close();
                                       }));
                           }));

                closeThread.Start();
            }
            catch
            {
                MessageBox.Show("Export process occur errors, probably because your computer is not installed Microsoft Powerpoint, please install the 2007 version and try again");
            }
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

       
    }
}
